
function addOnloads(fxn)
{

    var oldOnload = window.onload;
    if(typeof(oldOnload)=="function")
    {
        window.onload = function(){
            if(oldOnload){
                oldOnload();
            }
        }
        fxn();

    }else
        window.onload = fxn;
}

function initForms()
{

    for (var i=0; i< document.forms.length; i++) {

        if(document.forms[i].className.indexOf("validate")>-1)
        {
            document.forms[i].onsubmit =  validForm;
        }
    }
}

//Toggle the give given to it
function toggleDiv(divId)
{
    var displayStatus = document.getElementById(divId).style.display;
    if(displayStatus=="none")
    {
        $("#"+divId).show("slow");
    }else{
        $("#"+divId).hide("slow");
    }

}
function showdiv(divId)
{
    $(function(){
        $("#"+divId).show("slow");
        return( false );
    }
    );
}
function hidediv(divId)
{
    $(function(){
        $("#"+divId).hide("slow");
        return( false );
    }
    );
}
function hide(divId)
{
    $(function(){
        $("#"+divId).hide("scrollup");
        return( false );
    }
    );
}
function cleartxt(divId)
{
    document.getElementById(divId).value="";
}
function assigntxt(divId,txt)
{
    document.getElementById(divId).value=txt;
}

/*Form Functions*/
/*Checks and UnChecks CheckBoxes in a give form*/
function checkUncheck(frmname,prefix){
    var sid= prefix+"_0";
    var state = document.getElementById(sid).checked;
    for (var i=0; i< document.forms.length; i++) {
        if(document.forms[i].name==frmname)
        {
            var allTags = document.forms[i];
            for (var j=0; j<allTags.length; j++) {
                if (allTags[j].name.indexOf(prefix)>-1) {
                    allTags[j].checked = state;
                }
            }
        }
    }
}

/* User Comments*/

function setUserComment(comment_id,title){
    document.getElementById("user_comment_response").value=comment_id;
    document.getElementById("comment_title").innerHTML="<h3>"+title+"<span style='float:right'><a href='#add_comment' onclick='unsetComment()'>cancel reply</a></span></h3>";
}

function unsetComment(){
    document.getElementById("user_comment_response").value="";
    document.getElementById("comment_title").innerHTML="";
}

/* End User Comment*/
/*Validate Form*/
function validForm() {
    var allGood = true;
    var allTags = this.elements;


    for (var i=0; i<allTags.length; i++) {
        if (!validTag(allTags[i])) {
            allGood = false;
            document.getElementById("ermsg").style.display ="block";
            document.getElementById("ermsg").innerHTML="There are some errors with the form please check it";
            document.getElementById("ermsg").className+=" errortxt";
        }
    }
    return allGood;


    function validTag(thisTag) {
        var outClass = "";
        var allClasses = thisTag.className.split(" ");


        for (var j=0; j<allClasses.length; j++) {
            outClass += validBasedOnClass(allClasses[j]) + " ";
        }

        thisTag.className = outClass;


        if (outClass.indexOf("invalid") > -1) {
            //invalidLabel(thisTag.parentNode);
            invalidLabel(thisTag);
            thisTag.focus();
            if (thisTag.nodeName == "input") {
                inlineMsg("lname",'<strong>Error</strong><br />You must enter your name.',2);
                thisTag.select();
            }
            return false;
        }
        return true;


        function validBasedOnClass(thisClass) {
            var ranges, minRange;
            if(thisClass.substr(0,5) == "range"){
                ranges = (thisClass.substr(5));

                thisClass= "range";

            }
            var classBack = "";
            switch(thisClass) {
                case "":
                case "invalid":
                    break;
                case "reqd":
                    if (allGood && thisTag.value == "") {
                        // alert(thisTag.value );
                        classBack = "invalid ";
                    }
                    classBack += thisClass;
                    break;
                case "radio":
                    if (allGood && !radioPicked(thisTag.name)) {
                        classBack = "invalid ";
                    }
                    classBack += thisClass;
                    break;
                case "checked":
                    if (! thisTag.checked) {
                        classBack = "invalid ";
                    }
                    classBack += thisClass;
                    break;
                case "range":
                    if(allGood && !validateRange(ranges,thisTag.value)) {
                        classBack = "invalid ";
                    }
                    classBack += thisClass+ranges;
                    break;

                case "email":
                    if (allGood && !validEmail(thisTag.value)) {
                        classBack = "invalid ";
                    }
                    classBack += thisClass;
                    break;
                default:
                    //alert(thisClass);
                    /*if (allGood && !crossCheck(thisTag,thisClass)) {
						classBack = "invalid ";
					}*/
                    classBack += thisClass;
            }
            return classBack;
        }

        function crossCheck(inTag,otherFieldID) {
            if (!document.getElementById(otherFieldID)) {
                return false;
            }
            return (inTag.value == document.getElementById(otherFieldID).value);
        }

        function radioPicked(radioName) {
            var radioSet = "";

            for (var k=0; k<document.forms.length; k++) {
                if (!radioSet) {
                    radioSet = document.forms[k][radioName];
                }
            }
            if (!radioSet) return false;
            for (k=0; k<radioSet.length; k++) {
                if (radioSet[k].checked) {
                    return true;
                }
            }
            return false;
        }

        function validEmail(email) {
            var invalidChars = " /:,;";

            if (email == "") {
                return false;
            }
            for (var k=0; k<invalidChars.length; k++) {
                var badChar = invalidChars.charAt(k);
                if (email.indexOf(badChar) > -1) {
                    return false;
                }
            }
            var atPos = email.indexOf("@",1);
            if (atPos == -1) {
                return false;
            }
            if (email.indexOf("@",atPos+1) != -1) {
                return false;
            }
            var periodPos = email.indexOf(".",atPos);
            if (periodPos == -1) {
                return false;
            }
            if (periodPos+3 > email.length)	{
                return false;
            }
            return true;
        }
        function validateRange(range,value){
            var ranges = range.split("-");

            minRange = ranges[0];
            maxRange = ranges[1];
            if(maxRange == null || maxRange.length()<1)
            {
                if(value.length() < minRange)
                    return false;
            }else{
                if(value.length() < minRange && value.length() > maxRange ) return false;
            }
            return true;
        }

        function invalidLabel(parentTag) {
            if (parentTag.nodeName == "LABEL") {
                parentTag.className += " invalid";
            }
        }
    }
}
/*End form Validation*/
/*End Form Functions*/
/*Quick Link*/
function quickLink(val){
    location.href="http://"+val;
}
/*End Quick Link*/

/*
 *Ajax Post and Get Functions
 **/

function getData(dataSource,divId)
{
    document.getElementById(divId).style.display="block";
    document.getElementById(divId).innerHTML="<img src='images/load_pik.gif'> Loading..";
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        alert("Your browser does not support XMLHTTP!");
    }
    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState==4)
        {
            var otherPanel ="";
            if(divId=="changeDiv")
                otherPanel ="<div class='btop'><a href='#' onclick=\"hidediv('changeDiv')\" class='fright'>Close</a></top>"
            document.getElementById(divId).innerHTML = xmlhttp.responseText+otherPanel;

        }
    }
    xmlhttp.open("GET",dataSource,true);
    xmlhttp.send(null);
}

function getPostData(dataSource,data,divId){
    if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHttp");
    }

    if(XMLHttpRequestObject) {
        XMLHttpRequestObject.open("POST", dataSource);
        XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        XMLHttpRequestObject.onreadystatechange = function()
        {

            if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
                document.getElementById(divId).style.visibility = "visible";
                if(XMLHttpRequestObject.responseText == "login")
                    location="./?content=download"
                else
                    document.getElementById(divId).innerHTML = XMLHttpRequestObject.responseText;

            }
        }
        XMLHttpRequestObject.send(data);
    }
}
/* *End of Ajax Functions  **/
/*Typewriter JS*/
function changeTxt(pos,num,divid){
    pos=pos%txt.length;
    num=num%txt.length;
    window.setTimeout("changeTxtArray("+(pos+1)+","+(num)+",'"+divid+"')",200);
}
function changeTxtArray(pos,num,divid){
    pos=pos%txt[num][0].length;

    document.getElementById(divid).innerHTML="<a href='."+txt[num][1]+"' style='color:black;'>"+  txt[num][0].substring(0,pos+1)+"_</a>";
    num=num%txt.length;

    if(pos == txt[num][0].length - 1){
        window.setTimeout("changeTxt("+(0)+","+(num+1)+",'"+divid+"')",2000);
    }
    else
    {
        window.setTimeout("changeTxtArray("+(pos+1)+","+(num)+",'"+divid+"')",200);
    }
}
/*End Typewriter*/

/*Slider JS*/
// handles section to section scrolling of the content //
var SLIDETIMER = 3;
var SLIDESPEED = 3;
var SCROLLTIMER = 3;
var SCROLLSPEED = 3;
var STARTINGOPACITY = 40;
function slideContent(id,prefix,timer) {
    var div = document.getElementById(id);
    var slider = div.parentNode;
    clearInterval(slider.timer);
    slider.section = parseInt(id.replace(/\D/g,''));
    slider.target = div.offsetTop;
    slider.style.top = slider.style.top || '0px';
    slider.current = slider.style.top.replace('px','');
    slider.direction = (Math.abs(slider.current) > slider.target) ? 1 : -1;
    slider.style.opacity = STARTINGOPACITY * .01;
    slider.style.filter = 'alpha(opacity=' + STARTINGOPACITY + ')';
    slider.timer = setInterval( function() {
        slideAnimate(slider,prefix,timer)
    }, SLIDETIMER);
}

function slideAnimate(slider,prefix,timer) {
    var curr = Math.abs(slider.current);
    var tar = Math.abs(slider.target);
    var dir = slider.direction;
    if((tar - curr <= SLIDESPEED && dir == -1) || (curr - tar <= SLIDESPEED && dir == 1)) {
        slider.style.top = (slider.target * -1) + 'px';
        slider.style.opacity = 1;
        slider.style.filter = 'alpha(opacity=100)';
        clearInterval(slider.timer);
        if(slider.autoscroll) {
            setTimeout( function() {
                autoScroll(slider.id,prefix,timer)
            }, timer * 1000);
        }
    } else {
        var pos = (dir == 1) ? parseInt(slider.current) + SLIDESPEED : slider.current - SLIDESPEED;
        slider.current = pos;
        slider.style.top = pos + 'px';
    }
}

// handles manual scrolling of the content //
function scrollContent(id,dir) {
    var div = document.getElementById(id);
    clearInterval(div.timer);
    var sections = div.getElementsByTagName('div');
    var length = sections.length;
    var limit;
    if(dir == -1) {
        limit = 0;
    } else {
        if(length > 1) {
            limit = sections[length-1].offsetTop;
        } else {
            limit = sections[length-1].offsetHeight - div.parentNode.offsetHeight + 20;
        }
    }
    div.style.opacity = STARTINGOPACITY * .01;
    div.style.filter = 'alpha(opacity=' + STARTINGOPACITY + ')';
    div.timer = setInterval( function() {
        scrollAnimate(div,dir,limit)
    }, SCROLLTIMER);
}

function scrollAnimate(div,dir,limit) {
    div.style.top = div.style.top || '0px';
    var top = div.style.top.replace('px','');
    if(dir == 1) {
        if(limit - Math.abs(top) <= SCROLLSPEED) {
            cancelScroll(div.id);
            div.style.top = '-' + limit + 'px';
        } else {
            div.style.top = top - SCROLLSPEED + 'px';
        }
    } else {
        if(Math.abs(top) - limit <= SCROLLSPEED) {
            cancelScroll(div.id);
            div.style.top = limit + 'px';
        } else {
            div.style.top = parseInt(top) + SCROLLSPEED + 'px';
        }
    }
}

// cancel the scrolling on mouseout //
function cancelScroll(id) {
    var div = document.getElementById(id);
    div.style.opacity = 1;
    div.style.filter = 'alpha(opacity=100)';
    clearTimeout(div.timer);
}

// initiate auto scrolling //
function autoScroll(id,prefix,timer,restart) {
    var div = document.getElementById(id);
    div.autoscroll = (!div.autoscroll && !restart) ? false : true;
    if(div.autoscroll) {
        var sections = div.getElementsByTagName('div');
        var length = sections.length;
        div.section = (div.section && div.section < length) ? div.section + 1 : 1;
        slideContent(prefix + '-' + div.section,prefix,timer);
    }
}

// cancel automatic scrolling //
function cancelAutoScroll(id) {
    var div = document.getElementById(id);
    div.autoscroll = false;
}
/*End Slider JS*/

function processResourceDeleteClick(divId,val,content)
{
    var pnode =document.getElementById(divId).parentNode;
    var snode = pnode.parentNode;
    if(confirm('Would you like to remove this Resource from this Page ')){
        snode.style.display="none";

        getData("pages/resourcesaction.php?aj&d&r="+val+"&c="+content,'res_action');
    }
}
function processCommentDeleteClick(divId,content,cd,actDiv)
{
	var pnode =document.getElementById(divId).parentNode;
	var snode = pnode.parentNode;
	if(confirm('Would you like to remove this Comment from this Page ')){
	snode.style.display="none";
	
        getData("pages/resourcesaction.php?aj&"+cd+"&r=&c="+content,actDiv);
	}
}